package com.huihui.activity;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.huihui.R;
import com.huihui.adapter.DetialAdapter;
import com.huihui.adapter.MineZanGridViewAdapter;
import com.huihui.base.BaseActivity;
import com.huihui.bean.Article;
import com.huihui.bean.Comment;
import com.huihui.bean.Goods;
import com.huihui.bean.MineZan;
import com.huihui.bean.Photo;
import com.huihui.bean.ZuJi;
import com.huihui.bean.ZuJiInfo;
import com.huihui.datamanager.AppManager;
import com.huihui.eventbus.PublicRefrash;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.utils.IntentUtils;
import com.huihui.utils.LoadImageUtil;
import com.huihui.utils.ShareUtils;
import com.huihui.widget.NoScrollGridView;
import com.huihui.widget.SwipeRecyclerView;
import com.huihui.widget.dialog.ReplyOrCopyDialog;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

public class DetialActivity extends BaseActivity implements View.OnClickListener {
    //每次取多少条数据
    private int pageSize = 10;
    //	当前是第几次取数据
    private int currentPage = 1;
    private List<Comment> mList = new ArrayList<>();
    private Goods mGoods;
    private Article article;
    private long prod_id;
    private long art_id;
    private DetialAdapter commentAdapter;
    private SwipeRecyclerView recycelView;
    private Comment currentComment;
    private ReplyOrCopyDialog recplyAndCopyDialog;
    private LinearLayout inputLayout;
    private EditText inputText;
    private LinearLayout bottomLayout;
    private LinearLayout shareLayout, pinglunLaoyot, dianZanLayout;
    private ImageView zanIcon;
    private boolean zanFlag = false;
    private int isLike;
    private MineZan mineZan;
    private int class_id;
    private int currentPositon;
    private TextView commentNum;
    private ZuJiInfo zuJiInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detial);
        getIntentData();
        initView();
        initEvent();

    }

    private void initView() {
        inputLayout = findViewById(R.id.inuput_layout);
        inputText = findViewById(R.id.input_text);
        View headView = LayoutInflater.from(this).inflate(R.layout.activity_detail_head_layout, null);
        bottomLayout = findViewById(R.id.detail_bottom_layout);
        dianZanLayout = findViewById(R.id.detail_dian_zan_layout);
        zanIcon = findViewById(R.id.detail_zan_icon);
        shareLayout = findViewById(R.id.share_layout);
        pinglunLaoyot = findViewById(R.id.detail_pinglun_layout);
        pinglunLaoyot = findViewById(R.id.detail_pinglun_layout);
        CircleImageView imageView = headView.findViewById(R.id.head_view);
        TextView name = headView.findViewById(R.id.user_name);
        TextView time = headView.findViewById(R.id.user_time);
        TextView content = headView.findViewById(R.id.desc);
        NoScrollGridView imageGirdview = headView.findViewById(R.id.gridview);
        commentNum = headView.findViewById(R.id.comment_num);
        if (mGoods != null) {
            isLike = mGoods.getIs_like();
            //加载用户头像
            LoadImageUtil.displayImage(this, mGoods.getUser_head_photo(), imageView);
            name.setText(mGoods.getUser_nick_name());
            time.setText(mGoods.getUpdate_time());
            content.setText(mGoods.getProd_content());
            commentNum.setText(String.valueOf(mGoods.getComment_num()));
            MineZanGridViewAdapter detailPhotoAdapter = new MineZanGridViewAdapter(mGoods.getProd_image_url(), this);
            imageGirdview.setAdapter(detailPhotoAdapter);
        }

        if (article != null) {
            //加载用户头像
            isLike = article.getIs_like();
            LoadImageUtil.displayImage(this, article.getHead_img(), imageView);
            name.setText(article.getUser_nick_name());
            time.setText(article.getArt_time());
            content.setText(article.getArt_content());
            commentNum.setText(String.valueOf(article.getComment_num()));
            MineZanGridViewAdapter detailPhotoAdapter = new MineZanGridViewAdapter(article.getArt_content_url(), this);
            imageGirdview.setAdapter(detailPhotoAdapter);
        }

        if (mineZan != null) {
            isLike = mineZan.getIs_like();
            LoadImageUtil.displayImage(this, mineZan.getAuthor_head_img(), imageView);
            name.setText(mineZan.getAuthor_nickname());
            time.setText(mineZan.getTime());
            content.setText(mineZan.getContent());
            commentNum.setText(String.valueOf(mineZan.getComment_num()));
            MineZanGridViewAdapter detailPhotoAdapter = new MineZanGridViewAdapter(mineZan.getContent_url(), this);
            imageGirdview.setAdapter(detailPhotoAdapter);
        }
        if (zuJiInfo != null) {
            isLike = zuJiInfo.getIs_like();
            LoadImageUtil.displayImage(this, zuJiInfo.getAuthor_head_img(), imageView);
            name.setText(zuJiInfo.getName());
            time.setText(zuJiInfo.getTime());
            content.setText(zuJiInfo.getContent());
            commentNum.setText(String.valueOf(zuJiInfo.getComment_num()));
            List<String> photoList = new ArrayList<>();
            for (Photo p :
                    zuJiInfo.getPhotoList()) {
                photoList.add(p.getPath());
            }
            MineZanGridViewAdapter detailPhotoAdapter = new MineZanGridViewAdapter(photoList, this);
            imageGirdview.setAdapter(detailPhotoAdapter);
        }
        if (isLike == 1) {
            zanIcon.setImageResource(R.mipmap.zan_press);
        } else {
            zanIcon.setImageResource(R.mipmap.zan);
        }
        recycelView = (SwipeRecyclerView) findViewById(R.id.detail_swipe);
        recycelView.setRefreshing(true);
        setRecycleView(recycelView);
        long relation_id = 0;
        class_id = 0;
        if (mGoods != null) {
            relation_id = mGoods.getProd_id();
            class_id = 0;
        }
        if (article != null) {
            relation_id = article.getArt_id();
            class_id = 2;
        }
        if (mineZan != null) {
            class_id = mineZan.getClass_id();

            relation_id = mineZan.getRelation_id();
        }
        if (zuJiInfo != null) {
            class_id = zuJiInfo.getClass_id();
            relation_id = zuJiInfo.getZujiId();
        }
        commentAdapter = new DetialAdapter(this, mList, relation_id, class_id, isLike);
        commentAdapter.addHeader(headView);
        recycelView.setAdapter(commentAdapter);
        getProductComment();
    }

    public void setRecycleView(SwipeRecyclerView recyclerView) {
        //set layoutManager
        recyclerView.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
    }

    private void initEvent() {
        pinglunLaoyot.setOnClickListener(this);
        inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {//搜索按键action
                    hideInput(getContext());
                    int commentId = 0;
                    if (currentComment != null) {
                        commentId = currentComment.getCom_id();
                        replyComment(inputText.getText().toString(), commentId);
                    } else {
                        addProductComment(inputText.getText().toString());
                    }

                    showBottomLayout();
                }
                return false;
            }
        });
        recycelView.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getProductComment();
            }

            @Override
            public void onLoadMore() {
//                recycelView.showFooter();
                currentPage++;
                getProductComment();
            }
        });
        commentAdapter.setOnItemClickListener(new DetialAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                //点击监听 评论
                showReplyOrCopyDialog(position);
            }

            @Override
            public void onLongClick(int position) {
                //长按
            }
        });
        shareLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSharePop();
            }


        });
        recycelView.getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                showBottomLayout();
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        //对内容点赞
        dianZanLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int isLike = 0;
                if (mGoods != null) {
                    isLike = mGoods.getIs_like();
                } else if (article != null) {
                    isLike = article.getIs_like();
                } else if (mineZan != null) {
                    isLike = mineZan.getIs_like();
                }
                if (isLike == 1) {
                    removeZan();

                } else {

                    addZan(1);

                }
            }
        });
    }


    private void getIntentData() {
        Intent intent = getIntent();
        currentPositon = intent.getIntExtra("position", 0);
        mGoods = (Goods) intent.getSerializableExtra("goods");
        article = (Article) intent.getSerializableExtra("article");
        zuJiInfo = (ZuJiInfo) intent.getSerializableExtra("zuJiInfo");
        //商品唯一编码
        if (mGoods != null) {
            prod_id = mGoods.getProd_id();
//            getSupportActionBar().setTitle(mGoods.getUser_nick_name());
            titleView.setText(mGoods.getUser_nick_name());
        }

        if (article != null) {
            art_id = article.getArt_id();
//            getSupportActionBar().setTitle(article.getUser_nick_name());
            titleView.setText(article.getUser_nick_name());
        }
        mineZan = (MineZan) intent.getSerializableExtra("MineZan");

        if (mineZan != null) {
            class_id = mineZan.getClass_id();
            if (class_id == 0) {
                prod_id = mineZan.getRelation_id();

            } else if (class_id == 1) {

            } else if (class_id == 2) {
                art_id = mineZan.getRelation_id();
            }
//            getSupportActionBar().setTitle(mineZan.getAuthor_nickname());
            titleView.setText(mineZan.getAuthor_nickname());
        }
        if (zuJiInfo != null) {
            class_id = zuJiInfo.getClass_id();
            if (class_id == 0) {
                prod_id = zuJiInfo.getZujiId();
            } else if (class_id == 1) {

            } else if (class_id == 2) {
                art_id = zuJiInfo.getZujiId();
            }
//            getSupportActionBar().setTitle(zuJiInfo.getName());
            titleView.setText(zuJiInfo.getName());
        }
    }

    @Override
    public void onBackPressed() {
        saveZuJiInfo();

        super.onBackPressed();
    }

    private void refrashDataList() {
        EventBus.getDefault().post(new PublicRefrash(currentPositon, mGoods, article, mineZan));
    }

    private void saveZuJiInfo() {
        List<Photo> photoList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
        ZuJi zuJi = new ZuJi();
        List<ZuJi> zuJiList = DataSupport.where("time = ?", sdf.format(new Date())).find(ZuJi.class);
        if (zuJiList.size() > 0) {
            //存在
        } else {
            //不存在
            zuJi.setTime(sdf.format(new Date()));
            zuJi.save();
        }

        ZuJiInfo zuJiInfo = new ZuJiInfo();
        zuJiInfo.setCuttentTime(sdf.format(new Date()));
        if (mGoods != null) {
            prod_id = mGoods.getProd_id();
            zuJiInfo.setClass_id(0);
            zuJiInfo.setZujiId(prod_id);
            zuJiInfo.setContent(mGoods.getProd_content());
            zuJiInfo.setName(mGoods.getUser_nick_name());
            if (mGoods.getProd_image_url().size() > 0) {
                zuJiInfo.setIconPath(mGoods.getProd_image_url().get(0));
            } else {

                zuJiInfo.setIconPath(mGoods.getUser_head_photo());
            }
            zuJiInfo.setComment_num(mGoods.getComment_num());
            zuJiInfo.setAuthor_head_img(mGoods.getUser_head_photo());
            zuJiInfo.setTime(mGoods.getUpdate_time());
            zuJiInfo.setCuttentTime(sdf.format(new Date()));

            List<ZuJiInfo> list = DataSupport.where("class_id = ? and zujiId = ?", "0",
                    String.valueOf(mGoods.getProd_id())).find(ZuJiInfo.class);
            if (list.size() > 0) {
                ContentValues vaules = new ContentValues();
                vaules.put("cuttentTime", sdf.format(new Date()));
                DataSupport.updateAll(ZuJiInfo.class, vaules, "class_id = ? and zujiId = ?", "0",
                        String.valueOf(mGoods.getProd_id()));
            } else {
                zuJiInfo.save();
            }

            List<Photo> tempPhotoList = DataSupport.where("class_id = ? and photoId = ?", "0", String.valueOf(prod_id)).find(Photo.class);
            if (tempPhotoList.size() == 0) {
                List<String> prod_image_url = mGoods.getProd_image_url();
                for (String path : prod_image_url) {
                    Photo photo = new Photo();
                    photo.setClass_id(0);
                    photo.setPath(path);
                    photo.setPhotoId(prod_id);
                    photoList.add(photo);
                }
            }

        } else if (article != null) {
            art_id = article.getArt_id();
            zuJiInfo.setClass_id(2);
            zuJiInfo.setZujiId(art_id);
            zuJiInfo.setContent(article.getArt_content());
            zuJiInfo.setName(article.getUser_nick_name());
            if (article.getArt_content_url().size() > 0) {
                zuJiInfo.setIconPath(article.getArt_content_url().get(0));
            } else {

                zuJiInfo.setIconPath(article.getHead_img());
            }
            zuJiInfo.setComment_num(article.getComment_num());
            zuJiInfo.setAuthor_head_img(article.getHead_img());
            zuJiInfo.setTime(article.getArt_time());
            zuJiInfo.setCuttentTime(sdf.format(new Date()));
            List<ZuJiInfo> list = DataSupport.where("class_id = ? and zujiId = ?", "2",
                    String.valueOf(article.getArt_id())).find(ZuJiInfo.class);
            if (list.size() > 0) {
                ContentValues vaules = new ContentValues();
                vaules.put("cuttentTime", sdf.format(new Date()));
                DataSupport.updateAll(ZuJiInfo.class, vaules, "class_id = ? and zujiId = ?", "2",
                        String.valueOf(article.getArt_id()));
            } else {
                zuJiInfo.save();
            }
            List<Photo> tempPhotoList = DataSupport.where("class_id = ? and photoId = ?", "2", String.valueOf(art_id)).find(Photo.class);
            if (tempPhotoList.size() == 0) {
                List<String> art_content_url = article.getArt_content_url();
                for (String path : art_content_url) {
                    Photo photo = new Photo();
                    photo.setClass_id(2);
                    photo.setPath(path);
                    photo.setPhotoId(art_id);
                    photoList.add(photo);
                }

            }
        }
        DataSupport.saveAll(photoList);
    }

    private void getProductComment() {
        //商品
        if (class_id == 0) {
            OkHttpRequest.getInstance().getProductComment(HttpContants.product_getComment, prod_id, pageSize, currentPage, new CallBackUtil.CallBackString() {
                @Override
                public void onFailure(Call call, Exception e) {
                    recycelView.setRefreshing(false);
                }

                @Override
                public void onResponse(String response) {
                    recycelView.setRefreshing(false);
                    try {
                        JSONObject productObject = new JSONObject(response);
                        if (productObject != null && productObject.has(HttpContants.DATA)) {
                            List<Comment> list = AppManager.getGson().fromJson(productObject.getString(HttpContants.DATA), new TypeToken<List<Comment>>() {
                            }.getType());
                            if (list != null) {
                                if (currentPage == 1) {
                                    mList.clear();
                                    mList.addAll(list);
                                } else {
                                    mList.addAll(list);
                                    if (mList.size() < pageSize) {
                                        recycelView.setLoadMoreEnable(false);
                                    }
                                }
                                commentAdapter.notifyDataSetChanged();
                                if (list.size() < pageSize) {
                                    recycelView.setLoadMoreEnable(false);
                                }else{
                                    recycelView.setLoadMoreEnable(true);
                                }
                            } else {
                                recycelView.setLoadMoreEnable(false);
                            }

//                            recycelView.hideFooter();

                        } else {
//                            recycelView.setLoadMoreEnable(false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            //文章
        } else if (class_id == 2) {
            OkHttpRequest.getInstance().getArticleComment(HttpContants.article_getComment, art_id, pageSize, currentPage, new CallBackUtil.CallBackString() {
                @Override
                public void onFailure(Call call, Exception e) {
                    recycelView.setRefreshing(false);
                }

                @Override
                public void onResponse(String response) {
                    recycelView.setRefreshing(false);
                    try {
                        JSONObject productObject = new JSONObject(response);
                        if (productObject != null && productObject.has(HttpContants.DATA)) {
                            List<Comment> list = AppManager.getGson().fromJson(productObject.getString(HttpContants.DATA), new TypeToken<List<Comment>>() {
                            }.getType());
                            if (list != null) {
                                if (currentPage == 1) {
                                    mList.clear();
                                    mList.addAll(list);
                                } else {
                                    mList.addAll(list);
                                }
                                if (list.size() < pageSize) {
                                    recycelView.setLoadMoreEnable(false);
                                }else{
                                    recycelView.setLoadMoreEnable(true);
                                }
                                commentAdapter.notifyDataSetChanged();
                            } else {
                                recycelView.setLoadMoreEnable(false);
                            }
//                            recycelView.hideFooter();
                        } else {
//                            recycelView.setLoadMoreEnable(false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            //视频
        } else if (class_id == 1) {

        }

    }

    /**
     * 显示 回复，复制diloag
     *
     * @param position 评论 position
     */
    private void showReplyOrCopyDialog(int position) {
        currentComment = mList.get(position);
        if (recplyAndCopyDialog == null) {
            recplyAndCopyDialog = new ReplyOrCopyDialog(this);
            recplyAndCopyDialog.addClickListener(this);
        }
        recplyAndCopyDialog.show();

    }

    private void showSharePop() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_share_layout, null);
        final PopupWindow pop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        pop.setOutsideTouchable(true);
        pop.setFocusable(true);
        pop.setBackgroundDrawable(new ColorDrawable());
        pop.showAtLocation(inputLayout, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        backgroundAlpha(0.5f);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        view.findViewById(R.id.pop_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
                //有必要加下
            }
        });

        view.findViewById(R.id.wechat_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send(pop, 0);
            }
        });

        view.findViewById(R.id.wechat_cicle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send(pop, 1);
            }
        });
    }

    private void send(final PopupWindow pop, final int type) {
        int classId = 0;
        long relation_id = 0;
        if (article != null) {
            classId = 2;
            relation_id = article.getArt_id();
        } else if (mGoods != null) {
            classId = 0;
            relation_id = mGoods.getProd_id();
        } else if (zuJiInfo != null) {
            classId = class_id;
            relation_id = zuJiInfo.getZujiId();
        }
        pop.dismiss();
        //获取链接
        final int finalClassId = classId;
        OkHttpRequest.getInstance().getShareUrl(HttpContants.SHARE_URL, AppManager.getUserInfo().getUser_id(), classId, relation_id, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject != null) {
                        String url = jsonObject.getString("url");
                        if (mGoods != null) {
                            ShareUtils.shareSelect((Activity) getContext(), url, type, mGoods.getProd_content(), "信息来自好价," + mGoods.getUser_nick_name());
                        } else if (article != null) {
                            ShareUtils.shareSelect((Activity) getContext(), url, type, article.getArt_content(), "信息来自讨论," + mGoods.getUser_nick_name());
                        }else if(zuJiInfo != null){
                            if(finalClassId == 0){
                                ShareUtils.shareSelect((Activity) getContext(), url, type, zuJiInfo.getContent(), "信息来自好价," + zuJiInfo.getName());
                            }else{
                                ShareUtils.shareSelect((Activity) getContext(), url, type, zuJiInfo.getContent(), "信息来自讨论," + zuJiInfo.getName());
                            }
                        }
                        if (mGoods != null) {
                            mGoods.setShare_num(mGoods.getShare_num() + 1);
                        } else if (article != null) {
                            article.setShare_num(article.getShare_num() + 1);
                        } else if (mineZan != null) {
                            mineZan.setShare_num(mineZan.getShare_num() + 1);
                        }
                        refrashDataList();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reply_id:
                if (recplyAndCopyDialog.isShowing()) {
                    recplyAndCopyDialog.dismiss();
                }
                int com_id = currentComment.getCom_id();
                String commentName = currentComment.getUser_nick_name();
                showInputLayout("回复 " + commentName + ":");

                break;
            case R.id.copy_id:
                if (recplyAndCopyDialog.isShowing()) {
                    recplyAndCopyDialog.dismiss();
                }
                String commentCopy = currentComment.getCom_content();
                copyContent(commentCopy);
                break;

            case R.id.comment_id:
                if (recplyAndCopyDialog.isShowing()) {
                    recplyAndCopyDialog.dismiss();
                }
                int id = 0;
                if(class_id == 0){
                    id = 1;
                }else if(class_id == 2){
                    id = 4;
                }
                //评论  根据类型进行切换
                if(currentComment !=null) {
                    IntentUtils.gotoReportAnswerActivity(this, currentComment.getUser_nick_name(),currentComment.getUser_id(),currentComment.getCom_id(),id,currentComment.getCom_content());
                }
                break;
            case R.id.detail_pinglun_layout:
                //直接评论
                currentComment = null;
                showInputLayout("评论：");
                break;
            default:
                break;
        }
    }

    private void showInputLayout(String name) {
        bottomLayout.setVisibility(View.GONE);
        inputLayout.setVisibility(View.VISIBLE);
        inputText.setHint(name);
        showInput2(DetialActivity.this, inputText);
    }

    private void showBottomLayout() {
        inputText.getText().delete(0, inputText.getText().toString().length());
        inputLayout.setVisibility(View.GONE);
        bottomLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 回复评论
     * 二级评论
     *
     * @param com_id 被回复的评论id
     */
    private void replyComment(String content, int com_id) {
        if (class_id == 0) {
            addProductComment(content, com_id);
        } else if (class_id == 1) {

        } else if (class_id == 2) {
            addAriticleComment(content, com_id);
        }
    }


    /**
     * 直接评论，一级评论
     *
     * @param content 评论内容
     */
    private void addProductComment(String content) {
        if (class_id == 0) {
            addProductComment(content, 0);
        } else if (class_id == 1) {

        } else if (class_id == 2) {
            addAriticleComment(content, 0);
        }
    }

    /**
     * 复制文本
     *
     * @param content 需要复制的内容
     */
    private void copyContent(String content) {
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setPrimaryClip(ClipData.newPlainText(null, content));
        Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();
    }

    private void addAriticleComment(String content, final int parentCommentId) {
        OkHttpRequest.getInstance().addArticleComment(HttpContants.article_addComment, AppManager.getUserInfo().getUser_id(), art_id,
                parentCommentId, content, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject articleObject = new JSONObject(response);
                            if (articleObject != null && articleObject.has(HttpContants.STATE) && articleObject.getInt(HttpContants.STATE) == 10000) {
                                currentPage = 1;
                                getProductComment();
                                if (parentCommentId == 0) {
                                    if (article != null) {

                                        int comment_num = article.getComment_num();
                                        article.setComment_num(++comment_num);
                                        commentNum.setText(String.valueOf(article.getComment_num()));
                                    } else if (mineZan != null) {
                                        mineZan.setComment_num(mineZan.getComment_num() + 1);
                                        commentNum.setText(String.valueOf(mineZan.getComment_num()));
                                    }
                                    refrashDataList();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 添加评论
     *
     * @param content         评论内容
     * @param parentCommentId 父评论id （0 没有，其他 有）
     */
    private void addProductComment(String content, final int parentCommentId) {
        OkHttpRequest.getInstance().addProductComment(HttpContants.product_addComment, AppManager.getUserInfo().getUser_id(), prod_id,
                parentCommentId, content, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
                                currentPage = 1;
                                getProductComment();
                                if (parentCommentId == 0) {
                                    if (mGoods != null) {

                                        mGoods.setComment_num(mGoods.getComment_num() + 1);
                                        commentNum.setText(String.valueOf(mGoods.getComment_num()));
                                    } else if (mineZan != null) {
                                        mineZan.setComment_num(mineZan.getComment_num() + 1);
                                        commentNum.setText(String.valueOf(mineZan.getComment_num()));
                                    }
                                    refrashDataList();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void addZan(int addOrcancel) {
        //商品
        long relation_id = 0;
        if (mGoods != null) {
            relation_id = mGoods.getProd_id();
        }
        //点赞的评论ID（如果是内容点赞此处为0）
        int relation_comment_id = 0;
        if (article != null) {
            relation_id = article.getArt_id();
        }
        if (mineZan != null) {
            relation_id = mineZan.getRelation_id();
        }
        if (zuJiInfo != null) {
            relation_id = zuJiInfo.getZujiId();
        }
        OkHttpRequest.getInstance().addCollection(HttpContants.addCollection,
                AppManager.getUserInfo().getUser_id(),
                relation_id,
                class_id,
                relation_comment_id,
                new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
//                                currentPage = 1;
//                                getProductComment();
                                zanIcon.setImageResource(R.mipmap.zan_press);
                                if (mGoods != null) {

                                    mGoods.setCollection_num(mGoods.getCollection_num() + 1);
                                    mGoods.setIs_like(1);
                                } else if (article != null) {
                                    article.setCollection_num(article.getCollection_num() + 1);
                                    article.setIs_like(1);
                                } else if (mineZan != null) {
                                    mineZan.setCollection_num(mineZan.getCollection_num() + 1);
                                    mineZan.setIs_like(1);
                                }
                                refrashDataList();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void removeZan() {
        int comment_id = 0;
        long relation_id = 0;
        if (mGoods != null) {
            relation_id = mGoods.getProd_id();
        } else if (article != null) {
            relation_id = article.getArt_id();
        } else if (mineZan != null) {
            relation_id = mineZan.getRelation_id();
        }
        OkHttpRequest.getInstance().removeCollection(HttpContants.removeCollection, AppManager.getUserInfo().getUser_id(),
                relation_id, class_id, comment_id, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
                                zanIcon.setImageResource(R.mipmap.zan);
                                if (mGoods != null) {

                                    mGoods.setCollection_num(mGoods.getCollection_num() - 1);
                                    mGoods.setIs_like(0);
                                } else if (article != null) {
                                    article.setCollection_num(article.getCollection_num() - 1);
                                    article.setIs_like(0);
                                } else if (mineZan != null) {
                                    mineZan.setCollection_num(mineZan.getCollection_num() - 1);
                                    mineZan.setIs_like(0);
                                }
                                refrashDataList();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
